package com.ncu.week4;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{
    EditText foucus1;
    EditText foucus2;
    Button longclickbutton;
    EditText key1;
    EditText key2;
    TextView textview;
    private static final String ACTIVITY_TAG="Log";
    Button TouchButton;
    Button ContextMenuButton;
    TextView context_menu;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //给这个按钮设置长按监听
        longclickbutton = (Button) findViewById(R.id.button);
        longclickbutton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //输出消息
                Toast.makeText(MainActivity.this, "我是长按button", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        //聚焦
        foucus1 = (EditText) findViewById(R.id.editText);
        foucus1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus==true){
                    foucus1.setHint("现在聚焦于此");
                }else{
                    foucus1.setHint("现在聚焦于他处");
                }
            }
        });
        foucus2 = (EditText) findViewById(R.id.editText2);
        foucus2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus==true){
                    foucus2.setHint("现在聚焦于此");
                }else{
                    foucus2.setHint("现在聚焦于他处");
                }
            }
        });

        //按键监听
        key1 = (EditText) findViewById(R.id.editText4);
        key2 = (EditText) findViewById(R.id.editText5);
        textview = (TextView) findViewById(R.id.textView2);
        key2.setOnKeyListener(new EditText.OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                double x = Double.valueOf(key1.getText().toString());
                double y = Double.valueOf(key2.getText().toString());
                double z;
                try{
                    z= y/x;
                }catch (ArithmeticException exc){
                    Log.d(MainActivity.ACTIVITY_TAG,"计算错误");
                }
                textview.setText("BMI:"+y/x);
                return false;
            }
        });

        //触摸监听
        TouchButton = (Button) findViewById(R.id.button4);
        TouchButton.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        TouchButton.setText("按下");
                        break;
                    case MotionEvent.ACTION_UP:
                        TouchButton.setText("松开");
                        break;
                }
                return true;
            }
        });

        //上下文菜单按钮
        ContextMenuButton = (Button) findViewById(R.id.button2);
        ContextMenuButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                registerForContextMenu(v);
                openContextMenu(v);
                return true;
            }
        });
    }
    //选项菜单点击监听
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main_menu,menu);
        menu.findItem(R.id.menu_login).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "登录按钮按下", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        menu.findItem(R.id.menu_setting).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "设置按钮按下", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        menu.findItem(R.id.menu_exit).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "退出按钮按下", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.main_menu,menu);
        menu.findItem(R.id.menu_login).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "登录按钮按下", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        menu.findItem(R.id.menu_setting).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "设置按钮按下", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        menu.findItem(R.id.menu_exit).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "退出按钮按下", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }

}
